package com.insigma.business.qrcode.job._provider.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.insigma.business.qrcode.common.converter.CompanyProviderConverter;
import com.insigma.business.qrcode.common.converter.JobFairParticipateProviderConverter;
import com.insigma.business.qrcode.common.converter.JobFairProviderConverter;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.ResponseUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.job._provider.JobFairParticipateProvider;
import com.insigma.business.qrcode.job.dto.ActivityStatusEnum;
import com.insigma.business.qrcode.job.dto.JobCheckStatusEnum;
import com.insigma.business.qrcode.job.dto.JobFairParticipateDTO;
import com.insigma.business.qrcode.job.dto.request.JobFairParticipateInsertRequest;
import com.insigma.business.qrcode.job.dto.request.JobFairParticipateSearchRequest;
import com.insigma.business.qrcode.job.dto.request.JobFairParticipateUpdateRequest;
import com.insigma.business.qrcode.job.dto.request.JobFairSearchRequest;
import com.insigma.business.qrcode.job.dto.search.JobFairBSearcher;
import com.insigma.business.qrcode.job.dto.search.JobFairParticipateBSearcher;
import com.insigma.business.qrcode.job.entity.JobFairBO;
import com.insigma.business.qrcode.job.entity.JobFairDO;
import com.insigma.business.qrcode.job.service.JobFairService;
import com.insigma.business.qrcode.soldier.dto.CompanyDTO;
import com.insigma.business.qrcode.soldier.dto.OrgTypeEnum;
import com.insigma.business.qrcode.soldier.entity.JobFairParticipateDO;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import com.insigma.business.qrcode.soldier.service.JobFairParticipateService;
import com.insigma.business.qrcode.soldier.service.JobRecruitPositionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Component("jobFairParticipateProvider")
public class JobFairParticipateProviderImpl extends BaseProviderImpl implements JobFairParticipateProvider {

    @Autowired
    private JobFairParticipateService jobFairParticipateService;

    @Autowired
    private JobFairService jobFairService;
    @Autowired
    private JobRecruitPositionService jobRecruitPositionService;


    @Override
    public BasePagingResponse<List<JobFairParticipateDTO>> searchJobFairParticipate(final JobFairParticipateSearchRequest searchRequest) {

        final BasePagingResponse<List<JobFairParticipateDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页JobFairParticipate的请求参数不能为空");
            }

            @Override
            public void executeService() {
                if (Strings.isBlank(searchRequest.getSortKeyValueJoint())) {
                    searchRequest.setSortKeyValueJoint("createDatetime_desc");
                }

                final JobFairParticipateBSearcher jobFairParticipateBSearcher = JobFairParticipateProviderConverter.convertRequestSO2Searcher(searchRequest);
                final List<JobFairParticipateDO> jobFairParticipateDOList = jobFairParticipateService.search(jobFairParticipateBSearcher);
                final List<JobFairParticipateDTO> jobFairParticipateDTOList = JobFairParticipateProviderConverter.convertResponseDOList2DTOList(jobFairParticipateDOList);
                ResponseUtils.buildSuccessPagingResponse(basePagingResponse, jobFairParticipateDTOList, searchRequest, jobFairParticipateBSearcher.getTotalCount());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_SEARCH;
            }
        });

        return basePagingResponse;
    }

    @Override
    public BaseResponse<JobFairParticipateDTO> getParticipateByFairIdAndOrgId(String fairId, String companyId, String orgType) {

        BizCheckUtil.checkNotBlank(fairId, "招聘会Id不能为空");
        BizCheckUtil.checkNotBlank(companyId, "orgType公司Id不能为空");
        BizCheckUtil.checkNotBlank(orgType, "公司类型不能为空");

        JobFairParticipateDO jobFairParticipateDO = jobFairParticipateService.getParticipateByFairIdAndOrgId(fairId, companyId, orgType);
        final JobFairParticipateDTO remoteObj = JobFairParticipateProviderConverter.convertResponseDO2DTO(jobFairParticipateDO);

        return ResponseUtils.buildSuccessResponse(remoteObj);
    }

    @Override
    public BasePagingResponse<List<CompanyDTO>> searchCompanyOfFair(final JobFairSearchRequest request) {

        BizCheckUtil.checkNotNull(request, "请求不能为空");
        BizCheckUtil.checkNotBlank(request.getFairId(), "招聘会公司Id不能为空");

        if (Strings.isBlank(request.getSortKeyValueJoint())) {
            request.setSortKeyValueJoint("b.createDatetime_desc");
        }
        final JobFairBSearcher jobFairBSearcher = JobFairProviderConverter.convertRequestSO2Searcher(request);
        List<JobFairBO> jobFairBOS = jobFairService.searchCompanyOfFair(jobFairBSearcher);
        final List<CompanyDTO> companyDTOList = CompanyProviderConverter.convertResponseBOList2DTOList(jobFairBOS);
        return ResponseUtils.buildSuccessPagingResponse(companyDTOList, jobFairBSearcher);
    }


    @Override
    public BaseResponse<JobFairParticipateDTO> getJobFairParticipate(final BaseGetRequest baseGetRequest) {

        final BaseResponse<JobFairParticipateDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final JobFairParticipateDO jobFairParticipateDO = jobFairParticipateService.get((String) baseGetRequest.getId());
                JobFairParticipateDTO remoteObj = JobFairParticipateProviderConverter.convertResponseDO2DTO(jobFairParticipateDO);
//                List<JobRecruitPositionDO> list = jobRecruitPositionService.searchByOrgId((String) baseGetRequest.getId());
//                if (CollUtil.isNotEmpty(list)) {
//                    JobRecruitPositionDO jobRecruitPositionDO = list.get(0);
//                    BeanUtil.copyProperties(jobRecruitPositionDO, remoteObj, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
//                    remoteObj.setPositionId(jobRecruitPositionDO.getId());
//                }
                ResponseUtils.setupSuccessResponse(baseResponse, remoteObj);

            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertJobFairParticipate(final JobFairParticipateInsertRequest insertRequest) {

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getJobFairId(), "jobFairId不能为空");
            }

            @Override
            public void executeService() {
                JobFairDO jobFairDO = jobFairService.get(insertRequest.getJobFairId());
                BizCheckUtil.checkNotNull(jobFairDO, "招聘会不存在");
                BizCheckUtil.checkIsTrue(!(jobFairDO.getStatus().equals(ActivityStatusEnum.FINISHED.name())), "招聘已结束");

                JobFairParticipateDO oldOne = jobFairParticipateService.getParticipateByFairIdAndOrgId(insertRequest.getJobFairId(), insertRequest.getOrgId(), OrgTypeEnum.COMPANY.name());
                BizCheckUtil.checkIsTrue(oldOne == null, "已报名");
                final JobFairParticipateDO jobFairParticipateDO = JobFairParticipateProviderConverter.convertRequestIO2DO(insertRequest);
                jobFairParticipateService.insert(jobFairParticipateDO);
                ResponseUtils.setupSuccessResponse(baseResponse, jobFairParticipateDO.getId());
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_INSERT;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> reApplyJobFair(JobFairParticipateUpdateRequest updateRequest) {

        BizCheckUtil.checkNotNull(updateRequest, "新增JobFairParticipate的请求参数不能为空");
        BizCheckUtil.checkNotNull(updateRequest.getJobFairId(), "jobFairId不能为空");
        BizCheckUtil.checkNotNull(updateRequest.getOrgId(), "orgId不能为空");
        JobFairParticipateDO oldPart = jobFairParticipateService.getParticipateByFairIdAndOrgId(updateRequest.getJobFairId(), updateRequest.getOrgId(), OrgTypeEnum.COMPANY.name());
        BizCheckUtil.checkNotNull(oldPart, "还未报名过");
        final JobFairParticipateDO jobFairParticipateDO = JobFairParticipateProviderConverter.convertRequestUO2DO(updateRequest);
        jobFairParticipateDO.setId(oldPart.getId());
        jobFairParticipateDO.setAuditStatus(JobCheckStatusEnum.AUDIT_WAIT.name());

        jobFairParticipateDO.setCreateDatetime(oldPart.getCreateDatetime());
        jobFairParticipateDO.setCreatorId(oldPart.getCreatorId());
        jobFairParticipateDO.setCreatorType(oldPart.getCreatorType());
        jobFairParticipateDO.setModifierId(updateRequest.getModifierId());
        jobFairParticipateDO.setModifierType(updateRequest.getModifierType());
        jobFairParticipateDO.setModifyDatetime(new Date());
        Integer update = jobFairParticipateService.update(jobFairParticipateDO);
        return ResponseUtils.buildSuccessResponse(String.valueOf(update));
    }


    @Override
    public BaseResponse<Integer> updateJobFairParticipate(final JobFairParticipateUpdateRequest updateRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(), "操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(), "操作人类型不能为空");
            }

            @Override
            public void executeService() {
                JobFairParticipateDO jobFairParticipateDO = JobFairParticipateProviderConverter.convertRequestUO2DO(updateRequest);
                Integer affectRowNum = jobFairParticipateService.update(jobFairParticipateDO);
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> updateBooth(JobFairParticipateUpdateRequest updateRequest) {

        BizCheckUtil.checkNotNull(updateRequest, "修改JobFairParticipate的请求参数不能为空");
        BizCheckUtil.checkNotNull(updateRequest.getJobFairBooth(), "席位不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getModifierId(), "操作人不能为空");
        BizCheckUtil.checkNotBlank(updateRequest.getModifierType(), "操作人类型不能为空");

        JobFairParticipateDO pdo = jobFairParticipateService.get(updateRequest.getId());
        BizCheckUtil.checkNotNull(pdo, "还未报名");
        BizCheckUtil.checkIsTrue(pdo.getAuditStatus().equals(JobCheckStatusEnum.SUCCESS.name()), "还未审核通过");
        pdo.setJobFairBooth(updateRequest.getJobFairBooth());
        pdo.setModifierId(updateRequest.getModifierId());
        pdo.setModifierType(updateRequest.getModifierType());
        Integer update = jobFairParticipateService.update(pdo);
        return ResponseUtils.buildSuccessResponse(update);
    }

    @Override
    public BaseResponse<Integer> checkApply(JobFairParticipateUpdateRequest request) {

        BizCheckUtil.checkNotNull(request, "修改JobFairParticipate的请求参数不能为空");
        BizCheckUtil.checkNotBlank(request.getId(), "id不能为空");
        BizCheckUtil.checkNotBlank(request.getAuditStatus(), "审核状态不能为空");
        JobCheckStatusEnum jobCheckStatusEnum = JobCheckStatusEnum.valueOf(request.getAuditStatus());
        BizCheckUtil.checkIsTrue(JobCheckStatusEnum.SUCCESS.equals(jobCheckStatusEnum) || JobCheckStatusEnum.REJECT.equals(jobCheckStatusEnum), "状态错误");

        JobFairParticipateDO jobFairParticipateDO = jobFairParticipateService.get(request.getId());
        BizCheckUtil.checkNotNull(jobFairParticipateDO, "不存在");
        jobFairParticipateDO.setAuditStatus(request.getAuditStatus());
        jobFairParticipateDO.setModifierType(request.getModifierType());
        jobFairParticipateDO.setModifierId(request.getModifierId());
        Integer update = jobFairParticipateService.update(jobFairParticipateDO);
        return ResponseUtils.buildSuccessResponse(update);
    }


    @Override
    public BaseResponse<Integer> deleteJobFairParticipate(final BaseDeleteRequest deleteRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(), "操作人类型不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getModifierId(), "操作人不能为空");
            }

            @Override
            public void executeService() {
                JobFairParticipateDO jobFairParticipateDO = jobFairParticipateService.get((String) deleteRequest.getId());
                final Integer affectRowNum = jobFairParticipateService.delete((String) deleteRequest.getId(), deleteRequest.getModifierType(), (String) deleteRequest.getModifierId());
                jobRecruitPositionService.deleteByOnlineFairOrg(jobFairParticipateDO.getOrgId());
                ResponseUtils.setupSuccessResponse(baseResponse, affectRowNum);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> checkFairRecruitPosition(JobFairParticipateUpdateRequest request) {

        BizCheckUtil.checkNotBlank(request.getId(), "id不能为空");

        JobFairParticipateDO jobFairParticipateDO = jobFairParticipateService.get(request.getId());
        if (jobFairParticipateDO == null || !jobFairParticipateDO.getAuditStatus().equals(JobCheckStatusEnum.AUDIT_WAIT.name())) {
            return ResponseUtils.buildSuccessResponse(0);
        }
        JobCheckStatusEnum jobCheckStatusEnum = JobCheckStatusEnum.valueOf(request.getAuditStatus());
        if (jobCheckStatusEnum == null) {
            return ResponseUtils.buildFailedResponse(null,ErrorCodeEnum.ILLEGAL_PARAM);
        }
        jobFairParticipateDO.setAuditStatus(jobCheckStatusEnum.name());
        jobFairParticipateDO.setAuditStatusRemark(request.getAuditStatusRemark());
        jobFairParticipateDO.setModifyDatetime(new Date());
        Integer affectRowNum = jobFairParticipateService.update(jobFairParticipateDO);
        return ResponseUtils.buildSuccessResponse(affectRowNum);
    }

    @Override
    public BaseResponse<JobFairParticipateDTO> getParticipateByOrgId(String orgId) {
        JobFairParticipateDO jobFairParticipateDO = jobFairParticipateService.getByOrgId(orgId);
        JobFairParticipateDTO remoteObj = JobFairParticipateProviderConverter.convertResponseDO2DTO(jobFairParticipateDO);
        return ResponseUtils.buildSuccessResponse(remoteObj);
    }
}
